package dbms.ypr;


import dbms.ypr.user.DataBasePermissions;
import dbms.ypr.user.TablePermissions;
import dbms.ypr.user.User;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Start {

    public static void main(String[] args) throws IOException {
        Path filePath = Paths.get(SQLConstant.getPath());
        if (Files.notExists(filePath)) {
            Files.createDirectories(filePath);
        }
        if (initApp()) return;
        Input.get();
    }

    private static boolean initApp(){
        Arrays.asList("===========欢迎使用===========",
                        "1.用户登录",
                        "2.用户注册",
                        "3.表空间分配",
                        "4.退出系统",
                        "",
                        "请输入命令：")
                .stream()
                .forEach(System.out::println);
        SQLConstant.setNowPath(SQLConstant.getPath());
        Scanner scanner = new Scanner(System.in);
        String cmd = scanner.nextLine();
        switch (cmd) {
            case "1": {
                reLogin(scanner);
                return false;
            }
            case "2": {
                signIn(scanner);
                reLogin(scanner);
                return false;
            }
            case "3": {
                reLogin(scanner);
                addPermissions(scanner);
                return false;
            }
            case "4": {
                System.out.println("再见，欢迎再次使用");
                return true;
            }
            default: {
                System.out.println("命令无效");
                initApp();
            }
        }
        return false;
    }

    //创建权限
    private static void addPermissions(Scanner scanner) {
        Arrays.asList("\t1.添加数据库权限",
                        "\t2.移除数据库权限",
                        "\t3.添加表权限",
                        "\t4.移除表权限",
                        "\t5.返回首页",
                        "",
                        "请输入命令：")
                .stream()
                .forEach(System.out::println);
        String cmd = scanner.nextLine();
        User loginUser = User.getLoginUser(SQLConstant.currentUsername);

        switch (cmd) {
            case "1": {
                System.out.println("请输入需要添加数据库权限：");
                String dbName = scanner.nextLine();
                if(loginUser.getDataBasePermissions() != null
                        && loginUser.getDataBasePermissions().size() > 0
                 && loginUser.getDataBasePermissions().stream().noneMatch(a->a.getDatabaseName().equals(dbName))){
                    loginUser.getDataBasePermissions().add(new DataBasePermissions(dbName));
                } else {
                    loginUser.setDataBasePermissions(new ArrayList<>(Collections.singletonList(new DataBasePermissions(dbName))));
                }
                User.updateUser(loginUser);
                System.out.println(SQLConstant.currentUsername+"用户添加数据库权限："+ dbName);
            } break;
            case "2": {
                System.out.println("请输入需要移除数据库权限：");
                String dbName = scanner.nextLine();
                if(loginUser.getDataBasePermissions() != null
                        && loginUser.getDataBasePermissions().size() > 0
                        && loginUser.getDataBasePermissions().stream().anyMatch(a->a.getDatabaseName().equals(dbName))){
                    loginUser.setDataBasePermissions(new ArrayList<>(loginUser.getDataBasePermissions().stream()
                            .filter(a->!a.getDatabaseName().equals(dbName)).collect(Collectors.toList())));
                }
                User.updateUser(loginUser);
                System.out.println(SQLConstant.currentUsername+"用户移除数据库权限："+ dbName);
            } break;
            case "3": {
                System.out.println("请输入需要添加表权限：");
                String tName = scanner.nextLine();
                if(loginUser.getTablePermissions() != null
                        && loginUser.getTablePermissions().size() > 0
                        && loginUser.getTablePermissions().stream().noneMatch(a->a.getTableName().equals(tName))){
                    loginUser.getTablePermissions().add(new TablePermissions(tName));
                } else {
                    loginUser.setTablePermissions(new ArrayList<>(Collections.singletonList(new TablePermissions(tName))));
                }
                User.updateUser(loginUser);
                System.out.println(SQLConstant.currentUsername+"用户添加表权限："+ tName);
            } break;
            case "4": {
                System.out.println("请输入需移除表权限：");
                String tName = scanner.nextLine();
                if(loginUser.getTablePermissions() != null
                        && loginUser.getTablePermissions().size() > 0
                        && loginUser.getTablePermissions().stream().anyMatch(a->a.getTableName().equals(tName))){
                    loginUser.setTablePermissions(new ArrayList<>(loginUser.getTablePermissions().stream()
                            .filter(a->!a.getTableName().equals(tName)).collect(Collectors.toList())));
                }
                User.updateUser(loginUser);
                System.out.println(SQLConstant.currentUsername+"用户移除表权限："+ tName);
            } break;
            case "5": {
                initApp();
                return;
            }
        }
        System.out.println("");
        addPermissions(scanner);
    }

    private static void reLogin(Scanner scanner){
        System.out.println("请输入登录用户名：");
        SQLConstant.currentUsername = scanner.nextLine();
        System.out.println("请输入登录密码：");
        if (!login(SQLConstant.currentUsername, scanner.nextLine())) {
            reLogin(scanner);
        }
    }

    private static boolean login(String username, String password){
        User loginUser = User.getLoginUser(username);
        if (loginUser != null && username.equals(loginUser.getUsername()) && password.equals(loginUser.getPassword())){
            System.out.println("------登录成功-----");
            System.out.println("");
            return true;
        }
        System.out.println("账号密码错误");
        return false;
    }

    private static void signIn(Scanner scanner){
        System.out.println("请输入登录用户名：");
        String username = scanner.nextLine();
        System.out.println("请输入登录密码：");
        String password = scanner.nextLine();
        if (User.createUser(username, password)) {
            System.out.println("------注册成功------");
        } else {
            System.out.println("注册失败");
        }
    }
}
